package com.fs.web.base.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.baseMapper.RoleMapper;
import com.fs.common.baseMapper.RoleRightMapper;
import com.fs.common.pojo.Role;
import com.fs.common.pojo.RoleRight;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.utils.PageHeper;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import com.fs.web.base.vo.SysPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ACE
 * @desc 角色controller
 * @creationTime 2019-04-21 11:11
 * @projectName weixinframe
 * @packageName com.fs.web.base.controller
 */
@Slf4j
@Controller
@RequestMapping("/console/role/rewrite")
public class RoleController {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleRightMapper roleRightMapper;

    @RequestMapping("/config")
    public String config(HttpServletRequest request){
        Map<String, Object> map = ApplicationUtils.getRequestDatas();
        String id = map.get("id").toString();
        List<String> list = roleRightMapper.findId(id);
        request.setAttribute("idList", JSON.toJSONString(list));
        return SysPath.BACK_PATH.concat("/role/config");
    }

    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse list(HttpServletRequest request){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            Page page = PageHeper.pageRequest(request);
            Page<Role> list = roleMapper.findList(page, map);
            return JsonResponse.success(DataGrid.gridBuilder(list));
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/addConfig")
    public JsonResponse addConfig(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String id = map.get("id").toString();
            map.remove("id");
            QueryWrapper<RoleRight> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",id);
            roleRightMapper.delete(wrapper);
            if(map.size() != 0){
                for (Object value : map.values()) {
                    String menuRingtId = value.toString();
                    RoleRight roleRight = new RoleRight();
                    roleRight.setRoleId(id);
                    roleRight.setMenuMotionId(menuRingtId);
                    roleRightMapper.insert(roleRight);
                }
            }
            return JsonResponse.success();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResponse delete(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String id = map.get("id").toString();
            QueryWrapper<RoleRight> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",id);
            List<RoleRight> roleRights = roleRightMapper.selectList(wrapper);
            for (RoleRight roleRight : roleRights) {
                roleRightMapper.deleteById(roleRight.getId());
            }
            roleMapper.deleteById(id);
            return JsonResponse.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/newAdd")
    public JsonResponse newAdd(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            Integer roleCode = Integer.parseInt(map.get("roleCode").toString());
            if(roleCode == 2){
                QueryWrapper<Role> wrapper = new QueryWrapper<>();
                wrapper.eq("role_code",roleCode);
                List<Role> roles = roleMapper.selectList(wrapper);
                if(!CollectionUtils.isEmpty(roles)){
                    return JsonResponse.error("该角色已存在，请先删除已存在角色");
                }
            }
            Role role = JSON.parseObject(JSON.toJSONString(map), Role.class);
            roleMapper.insert(role);
            return JsonResponse.success("新增成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/modify")
    public JsonResponse modify(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            Integer roleCode = Integer.parseInt(map.get("roleCode").toString());
            if(roleCode == 2){
                QueryWrapper<Role> wrapper = new QueryWrapper<>();
                wrapper.eq("role_code",roleCode);
                List<Role> roles = roleMapper.selectList(wrapper);
                if(!CollectionUtils.isEmpty(roles) && roles.size() > 1){
                    return JsonResponse.error("该角色已存在，请先删除已存在角色");
                }else{
                    if(!roles.get(0).getId().equals(map.get("id").toString())){
                        return JsonResponse.error("该角色已存在，请先删除已存在角色");
                    }
                }
            }
            Role role = JSON.parseObject(JSON.toJSONString(map), Role.class);
            roleMapper.updateById(role);
            return JsonResponse.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }
}